﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;
using FX.CC.Andon.Entity.Service;

namespace FX.CC.Andon.Service.Service
{
    public class MessageService : DbHelperBase
    {
        public IList<MessageWrapper> GetMessage(DateTime lastGetTime, out DateTime newGetTime)
        {
            string sql =
@"Select @dbGetTime = getdate();
SELECT Msg.MsgID, Msg.MsgType, Msg.EventID, WS.WSName, Ln.LineName, EQ.EqCode, EQ.EqName, 
      ET.ETypeName, Detail.DetailName, Usr.FullName, Rec.StartTime, Msg.GenTime, 
      ToUsr.MobileNo, ToUsr.FullName AS Receiver, SndUsr.FullName AS Sender
FROM UT_Message AS Msg INNER JOIN
      UT_EventRec AS Rec ON Msg.EventID = Rec.EventID INNER JOIN
      UT_Equipment AS EQ ON Rec.EqID = EQ.EqID INNER JOIN
      UT_EType AS ET ON Rec.ETypeCode = ET.ETypeCode INNER JOIN
      UT_ETypeDetail AS Detail ON Rec.DetailID = Detail.DetailID INNER JOIN
      UT_User AS Usr ON Rec.StartUserID = Usr.UserID INNER JOIN
      UT_Line AS Ln ON EQ.LineID = Ln.LineID INNER JOIN
      UT_WorkShop AS WS ON Ln.WSID = WS.WSID INNER JOIN
      UT_User AS ToUsr ON Msg.ToUserID = ToUsr.UserID INNER JOIN
      UT_User AS SndUsr ON Msg.FromUserID = SndUsr.UserID
WHERE (Msg.GenTime > @lastGetTime);";

            IList<MessageWrapper> list = new List<MessageWrapper>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@lastGetTime", DbType.DateTime, lastGetTime);
                Db.AddOutParameter(cmd, "@dbGetTime", DbType.DateTime);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new MessageWrapper();
                            obj.MsgID = ReaderHelper.GetValue<Guid>(reader,"MsgID");
                            obj.MsgTypeObj = (MsgType)(ReaderHelper.GetValue<int>(reader, "MsgType"));
                            obj.EventID = ReaderHelper.GetValue<Guid>(reader, "EventID");
                            obj.WSName = ReaderHelper.GetValue<string>(reader, "WSName");
                            obj.LineName = ReaderHelper.GetValue<string>(reader, "LineName");
                            obj.EqCode = ReaderHelper.GetValue<string>(reader, "EqCode");
                            obj.EqName = ReaderHelper.GetValue<string>(reader, "EqName");
                            obj.ETypeName = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            obj.DetailName = ReaderHelper.GetValue<string>(reader, "DetailName");
                            obj.StartTime = ReaderHelper.GetValue<DateTime>(reader, "StartTime");
                            obj.StartUserName = ReaderHelper.GetValue<string>(reader, "FullName");
                            obj.GenTime = ReaderHelper.GetValue<DateTime>(reader, "GenTime");
                            obj.Receiver = ReaderHelper.GetValue<string>(reader, "Receiver");
                            obj.MobileNo = ReaderHelper.GetValue<string>(reader, "MobileNo");
                            obj.Sender = ReaderHelper.GetValue<string>(reader, "Sender");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                newGetTime = (DateTime)cmd.Parameters["@dbGetTime"].Value;
                return list;
            }
        }
    }
}
